package com.kgc.springboot.aviation.service.gwd.Impl;

import com.kgc.springboot.aviation.mapper.UserMapper;
import com.kgc.springboot.aviation.mapper.UsersMapper;
import com.kgc.springboot.aviation.pojo.User;
import com.kgc.springboot.aviation.pojo.UserExample;
import com.kgc.springboot.aviation.pojo.Users;
import com.kgc.springboot.aviation.pojo.UsersExample;
import com.kgc.springboot.aviation.service.gwd.LoginService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : 郭未栋
 * @version : 1.0
 * @date ： Created in 2020/12/30
 * @description :
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Resource
    UserMapper userMapper;
    @Resource
    UsersMapper usersMapper;
    //管理员登录
    @Override
    public User login(String userName, String userPassword) {
        UserExample example=new UserExample();
        example.createCriteria().andUserPhoneEqualTo(userName).andUserPasswordEqualTo(userPassword);
        List<User> users = userMapper.selectByExample(example);
        if (users!=null&&users.size()>0){
            User user = users.get(0);
            return user;
        }
        return null;
    }
    //用户登录
    @Override
    public Users login2(String usersName, String usersPassword) {
        UsersExample example=new UsersExample();
        if (usersName!=null&&usersName!=""){
            example.createCriteria().andUsersNameEqualTo(usersName).andUsersPasswordEqualTo(usersPassword);
            List<Users> users = usersMapper.selectByExample(example);
            if (users!=null&&users.size()>0){
                Users users1 = users.get(0);
                System.out.println("用户名为："+users1.getUsersName());
                return users1;

            }
        }
//        if (usersPhone!=null&&usersPhone!=""){
//            example.createCriteria().andUsersPhoneEqualTo(usersPhone).andUsersPasswordEqualTo(usersPassword);
//            List<Users> users1 = usersMapper.selectByExample(example);
//            if (users1!=null&&users1.size()>0){
//                Users users2 = users1.get(0);
//                return users2;
//            }
//        }
        return null;
    }

    @Override
    public Users login3(String usersPhone) {
        UsersExample example = new UsersExample();
        example.createCriteria().andUsersPhoneEqualTo(usersPhone);
        List<Users> users = usersMapper.selectByExample(example);
        if (users.size()>0){
            Users users1 = users.get(0);
            return users1;
        }
        return null;
    }

    @Override
    public List<Users> select(String usersPhone) {
        UsersExample example=new UsersExample();
        if (usersPhone!=null&&usersPhone!=""){
            System.out.println("进来短信啊啊啊啊啊啊");
            example.createCriteria().andUsersPhoneEqualTo(usersPhone);
            List<Users> users = usersMapper.selectByExample(example);
            return users;
        }
        List<Users> users = usersMapper.selectByExample(null);
        return users;
    }

    @Override
    public Integer addUsers(Users Users) {
        int insert = usersMapper.insert(Users);
        return insert;
    }

    @Override
    public Integer upUsers(Users users) {
        String usersPhone = users.getUsersPhone();
        String usersPassword = users.getUsersPassword();
        System.out.println("修改后的密码为"+usersPassword);
        System.out.println("修改的手机号为"+usersPhone);
        UsersExample example=new UsersExample();
        example.createCriteria().andUsersPhoneEqualTo(users.getUsersPhone());
        int i = usersMapper.updateByExampleSelective(users, example);
        return i;
    }

    @Override//修改后台管理员密码
    public Integer upUser(User user) {
        UserExample example=new UserExample();
        example.createCriteria().andIdEqualTo(user.getId());
        int i = userMapper.updateByExampleSelective(user, example);
        return i;
    }

    @Override
    public User getUser(User user) {
        UserExample example=new UserExample();
        example.createCriteria().andIdEqualTo(user.getId());
        List<User> users = userMapper.selectByExample(example);
        if (users.size()>0&&users!=null){
            User user1 = users.get(0);
            return user1;
        }
        return null;
    }
}
